<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>角色</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>
<blockquote class="layui-elem-quote list_search">
        <div class="layui-inline">
            <a class="layui-btn layui-btn-normal roleAdd_btn"><i
                    class="layui-icon">&#xe608;</i> 添加角色</a>
        </div>
        <div class="layui-inline">
            <a class="layui-btn layui-btn-danger batchDel"
               data-type="delCheckData"><i class="layui-icon">&#xe640;</i>批量删除</a>
        </div>
</blockquote>
<table id="roleList" class="roleList" lay-filter="roleList"></table>

<script th:src="@{/src/layui.js}"></script>

<script type="text/html" id="delEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['jquery','layer','laypage','laytpl','table'],function () {
        var $ = layui.jquery,
            layer = layui.layer,
            laypage = layui.laypage,
            laytpl = layui.laytpl,
            table = layui.table;
        table.render({
            elem:'#roleList',
            url:'/roles/list',
            title:'角色列表',
            cellMinWidth:80,
            page:true,
            limit:10,
            limits:[10,20,30],
            cols:[
                [
                    {type:'checkbox'},
                    {title:'ID',field:'id'},
                    {title:'角色名',field:'roleName'},
                    {title:'角色描述',field:'duty'},
                    {title:'操作',templet:'#delEdit'}
                ]
            ],
            parseData:function(result){
                return {
                    code:result.code==200?0:result.code,
                    msg:result.message,
                    count:result.data.total,
                    data:result.data.list
                }
            }
        });
        //添加角色
        $(".roleAdd_btn").click(function () {
            var index = layer.open({
                type:2,
                title:'添加角色',
                content:'/sys/addRole'
            });
            //窗口大小发生改变的时候触发该事件
            $(window).resize(function () {
                layer.full(index);
            });
            layer.full(index);
        });
        //监听表格的行工具事件
        table.on('tool(roleList)',function (row) {
            var data =  row.data;//获取当前行的数据
            if(row.event === 'edit'){
                //打开编辑页面进行编辑
                layer.open({
                    type:2,
                    title:'编辑角色',
                    area:['500px','600px'],
                    content:'/roles/editRole/'+row.data.id,
                });
            }else if(row.event === 'del'){
                //删除操作
                layer.confirm('确认删除？',function (index) {
                    $.ajax({
                        url:'/roles/delete/'+data.id,
                        type:'delete',
                        dataType:'json',
                        success:function(result){
                            if(result.code == 200){
                                layer.msg('删除成功',{icon:1,time:1000},function () {
                                    parent.location.reload();
                                });
                            }else{
                                layer.msg(result.message,{icon:5});
                            }
                        }
                    })
                })
            }
        })
        //批量删除
        $(".batchDel").click(function () {
            var checkStatus = table.checkStatus("roleList");
            var data = checkStatus.data;
            if (data.length==0){
                layer.msg("请至少选择一行数据!"),{icon:5};
                return;
            }
            var  str="";
            var flag = false;
            $.each(data,function (index,object) {
                str += object.id+",";
                if (object.id==1){
                    flag = true;
                    return;
                }
            })
            if (flag){
                layer.msg("不能删除超级管理员!");
                return;
            }
            str = str.substring(0,str.length-1);
            layer.confirm("确认删除?",function(index) {
                $.ajax({
                    url:'/roles/batchDel/'+str,
                    type:'delete',
                    dataType:'json',
                    success:function (result) {
                        if (result.code==200){
                            layer.msg('删除成功',{icon:1,time:1000},function () {
                                location.reload();
                            })
                        }else{
                            layer.msg(result.message,{icon:5});
                        }
                    }
                })
                layer.close(index);
            })

        })


    })
</script>


</body>
</html>